﻿namespace SupportSystemUI.Controls.Basic
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using SupportSystemUI.Controls.CustomPages;
    using SupportSystemUI.Controls.Pages;

    /// <summary>
    /// Interaction logic for MainScreen.xaml
    /// </summary>
    public partial class MainScreen : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainScreen"/> class.
        /// </summary>
        public MainScreen()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Pushes a certain page ontop of the stack.
        /// </summary>
        /// <param name="func">A function when called, returns a <see cref="PageData"/> object to populate the new page with.</param>
        public void Push(Func<PageData> func)
        {
            this.pagesPanel.Push(new LoadingPage());
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (sender, args) => { args.Result = func(); };
            worker.RunWorkerCompleted += (sender, args) =>
            {
                this.pagesPanel.Push(new BasePage(args.Result as PageData));
            };

            worker.RunWorkerAsync();
        }

        /// <summary>
        /// Handler called when the control finishes loading.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Routed event arguments.</param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.logoutGrid.MouseUp += (sender2, args2) => { Global.Instance.MainSlidePanel.Pop(); };

            Action homeAction = () =>
            {
                this.pagesPanel.Clear();
                HomePage page = new HomePage();
                this.Push(() => new PageData(page, "Home", "Please review all late notifications and sign them."));
            };

            this.homeButton.AddClickAction(homeAction);

            homeAction();
        }
    }
}